/*
 * 
 * 
 * 
 */
package com.hboxs.asl.service.impl;

import com.hboxs.asl.Page;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.dao.FinancialRecordDao;
import com.hboxs.asl.entity.*;
import com.hboxs.asl.service.FinancialRecordService;
import com.hboxs.asl.service.QuestionService;
import com.hboxs.asl.service.SnService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * Service - 财务记录
 */
@Service("financialRecordServiceImpl")
public class FinancialRecordServiceImpl extends BaseServiceImpl<FinancialRecord, Long> implements FinancialRecordService {

    @Resource(name = "financialRecordDaoImpl")
    private FinancialRecordDao financialRecordDao;
    @Resource(name = "questionServiceImpl")
    private QuestionService questionService;
    @Resource(name = "snServiceImpl")
    private SnService snService;

    @Resource(name = "financialRecordDaoImpl")
    public void setBaseDao(FinancialRecordDao financialRecordDao) {
        super.setBaseDao(financialRecordDao);
    }

    /**
     * 易问易答支出记录
     * @param question
     */
    @Override
    public void expenditureRecordQuestion(Question question, String remark) {
        FinancialRecord financialRecord = new FinancialRecord();
        financialRecord.setSourse(FinancialRecord.Sourse.question);
        financialRecord.setStatus(FinancialRecord.Status.expenditureRecord);
        financialRecord.setRemark(remark);
        if (question.getPayPrice() != null) {
            financialRecord.setAmount(question.getPayPrice());
        }
        if (question.getMember() != null) {
            financialRecord.setMember(question.getMember());
        }
        if (question.getSn() != null) {
            financialRecord.setSn(question.getSn());
        }
        this.save(financialRecord);
    }

    /**
     * 易问易答收入记录
     * @param question
     */
    @Override
    public void incomeRecordQuestion(Question question, String remark) {
        FinancialRecord financialRecord = new FinancialRecord();
        financialRecord.setSourse(FinancialRecord.Sourse.question);
        financialRecord.setStatus(FinancialRecord.Status.incomeRecord);
        financialRecord.setRemark(remark);
        if (question.getPayPrice() != null) {
            financialRecord.setAmount(question.getPayPrice());
        }
        if (question.getTeacher() != null) {
            financialRecord.setMember(question.getTeacher());
        }
        if (question.getSn() != null) {
            financialRecord.setSn(question.getSn());
        }
        this.save(financialRecord);
    }

    /**
     * 课程支出记录
     * @param courseTrade
     */
    @Override
    public void expenditureRecordCourse(CourseTrade courseTrade, String remark) {
        FinancialRecord financialRecord = new FinancialRecord();
        financialRecord.setSourse(FinancialRecord.Sourse.live);
        financialRecord.setStatus(FinancialRecord.Status.expenditureRecord);
        financialRecord.setRemark(remark);
        if (courseTrade.getSn() != null) {
            financialRecord.setSn(courseTrade.getSn());
        }
        if (courseTrade.getPrice() != null) {
            financialRecord.setAmount(courseTrade.getPrice());
        }
        if (courseTrade.getBuyer() != null) {
            financialRecord.setMember(courseTrade.getBuyer());
        }
        this.save(financialRecord);
    }

    /**
     * 课程收入记录
     * @param courseTrade
     */
    @Override
    public void incomeRecordCourse(CourseTrade courseTrade, Member member, String remark) {
        FinancialRecord financialRecord = new FinancialRecord();
        financialRecord.setSourse(FinancialRecord.Sourse.live);
        financialRecord.setStatus(FinancialRecord.Status.incomeRecord);
        financialRecord.setRemark(remark);
        if (courseTrade.getSn() != null) {
            financialRecord.setSn(courseTrade.getSn());
        }
        if (courseTrade.getPrice() != null) {
            financialRecord.setAmount(courseTrade.getPrice());
        }
        financialRecord.setMember(member);
        this.save(financialRecord);
    }

    /**
     * 1元查看答案支出记录
     * @param yiYuanTrade
     */
    @Override
    public void expenditureRecordYiYuan(YiYuanTrade yiYuanTrade, String remark) {
        FinancialRecord financialRecord = new FinancialRecord();
        financialRecord.setSourse(FinancialRecord.Sourse.question);
        financialRecord.setStatus(FinancialRecord.Status.expenditureRecord);
        financialRecord.setRemark(remark);
        if (yiYuanTrade.getSn() != null) {
            financialRecord.setSn(yiYuanTrade.getSn());
        }
        if (yiYuanTrade.getBuyer() != null) {
            financialRecord.setMember(yiYuanTrade.getBuyer());
        }
        if (yiYuanTrade.getPrice() != null) {
            financialRecord.setAmount(yiYuanTrade.getPrice());
        }
        this.save(financialRecord);
    }

    /**
     * 1元查看答案提成收入记录
     * @param yiYuanTrade
     */
    @Override
    public void incomeRecordYiYuan(YiYuanTrade yiYuanTrade, String remark) {
        FinancialRecord financialRecord = new FinancialRecord();
        financialRecord.setSourse(FinancialRecord.Sourse.question);
        financialRecord.setStatus(FinancialRecord.Status.incomeRecord);
        financialRecord.setRemark(remark);
        if (yiYuanTrade.getSn() != null) {
            financialRecord.setSn(yiYuanTrade.getSn());
        }
        if (yiYuanTrade.getQuestionId() != null) {
            Question question = questionService.find(yiYuanTrade.getQuestionId());
            if (question != null) {
                financialRecord.setMember(question.getMember());
            }
        }
        if (yiYuanTrade.getPrice() != null) {
            financialRecord.setAmount(yiYuanTrade.getPrice().divide(BigDecimal.valueOf(2)));
        }
        this.save(financialRecord);
    }

    /**
     * 直播送礼支出记录
     */
    @Override
    public void expenditureLiveGifts(Member member, BigDecimal money) {
        FinancialRecord financialRecord = new FinancialRecord();
        financialRecord.setSn(snService.generate(Sn.Type.defray));
        financialRecord.setSourse(FinancialRecord.Sourse.liveGifts);
        financialRecord.setStatus(FinancialRecord.Status.expenditureRecord);
        financialRecord.setMember(member);
        financialRecord.setAmount(money);
        this.save(financialRecord);
    }

    /**
     * 直播送礼收入记录
     */
    @Override
    public void incomeLiveGifts(Member member, BigDecimal money) {
        FinancialRecord financialRecord = new FinancialRecord();
        financialRecord.setSn(snService.generate(Sn.Type.defray));
        financialRecord.setSourse(FinancialRecord.Sourse.liveGifts);
        financialRecord.setStatus(FinancialRecord.Status.incomeRecord);
        financialRecord.setMember(member);
        financialRecord.setAmount(money);
        this.save(financialRecord);
    }

    @Override
    public Page<FinancialRecord> incomeRecord(Pageable pageable, Member member) {
        return financialRecordDao.incomeRecord(pageable, member);
    }

    @Override
    public Page<FinancialRecord> expenditureRecord(Pageable pageable, Member member) {
        return financialRecordDao.expenditureRecord(pageable, member);
    }
}
